package com.qire.manhua.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.qire.common.basic.ObjectUtil;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled = true;
    private SmartTabLayout smartTabLayout;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
        super.setAdapter(adapter);
        if(ObjectUtil.nonNull(smartTabLayout)) {
            smartTabLayout.setViewPager(this);
        }
    }

    public void bindSmartTabLayout(SmartTabLayout smartTabLayout){
        this.smartTabLayout = smartTabLayout;
        smartTabLayout.setViewPager(this);
    }
}
